/**
 * @name plugin img_lazy_load/图片延迟加载 unit
 * @copyright    tcsos    inc
 * @author    周琪
 * @version    v1.0
 */
(function(undefined){
    var img_lazy_load_obj = {};
    
    //加载标签
    img_lazy_load_obj.load_atr = 'lazy_src';
    
    //暂停加载
    img_lazy_load_obj.lock = false;    
    
    //正在加载的数量
    img_lazy_load_obj.loading_number = 0;
    
    //最大加载数量
    img_lazy_load_obj.max_loading = 10;
    
    //清除元素
    img_lazy_load_obj.clear_element = function() {
        //所有的存储标签对象                
        if(img_lazy_load_obj.element_obj) {
            delete img_lazy_load_obj.element_obj;
        }
        img_lazy_load_obj.element_obj = {};
        
        //存储图片位置
        if(img_lazy_load_obj.img_map) {
            delete img_lazy_load_obj.img_map;
        }        
        img_lazy_load_obj.img_map = [];
        
        //是否加载了元素        
        img_lazy_load_obj.img_lazy_count = 0        
    }

    //初始化图片加载器
    //param.tags 标签
    img_lazy_load_obj.init = function(param) {
        if(img_lazy_load_obj.time_out) {
            clearTimeout(img_lazy_load_obj.time_out);
        }
        
        img_lazy_load_obj.doc_body = document.body;
        img_lazy_load_obj.doc_element = document.compatMode == 'BackCompat' ? img_lazy_load_obj.doc_body : document.documentElement;

        //所需加载的标签
        img_lazy_load_obj.lazy_label = param.tags || ['img', 'iframe'];
        
        //清除加载元素
        img_lazy_load_obj.clear_element();

        //获取加载元素
        img_lazy_load_obj.init_element();

        img_lazy_load_obj.flag = true;

        //定时器对象
        img_lazy_load_obj.time_out = '';

        //进行图片加载
        img_lazy_load_obj.time_out = setTimeout(img_lazy_load_obj.img_lazy_down, 200);
    }

    //获取加载的元素
    img_lazy_load_obj.init_element = function() {
        var el = '';
        for(var i = 0, c = img_lazy_load_obj.lazy_label.length; i < c; i++) {
            el = document.getElementsByTagName(img_lazy_load_obj.lazy_label[i]);
            for(var j = 0, c2 = el.length; j < c2; j++) {
                if(typeof(el[j]) != 'object' || !el[j].getAttribute(img_lazy_load_obj.load_atr)) {
                    continue;
                }    

                //获取图片高度
                img_top = img_lazy_load_obj.get_img_top(el[j]);

                //如果位置不存在，创建一个地图位置
                if(img_lazy_load_obj.element_obj[img_top] == undefined) {
                    var tmp_obj = [];
                    tmp_obj[0] = el[j];
                    img_lazy_load_obj.element_obj[img_top] = tmp_obj;
                    img_lazy_load_obj.img_lazy_count++;
                    img_lazy_load_obj.img_map.push(img_top);
                    continue;
                } 

                img_lazy_load_obj.element_obj[img_top].push(el[j]);
            }
        }
    }

    //进行图片加载
    img_lazy_load_obj.img_lazy_down = function() {
        if(img_lazy_load_obj.lock) {
            return;
        }
        
        if(img_lazy_load_obj.img_lazy_count < 1) {
            clearTimeout(img_lazy_load_obj.time_out);
            return;
        }

        if(!img_lazy_load_obj.flag) {
            return;
        }

        //滚动条高度
        var scr_top  = img_lazy_load_obj.doc_element.scrollTop + document.body.scrollTop;

        //实际高度
        var show_height = img_lazy_load_obj.doc_element.clientHeight;

        //可视化最高度
        var vis_height = scr_top + show_height;

        //可视化最低高度
        var vis_top_height = vis_height - show_height;

        //循环加载坐标图片（重点）
        for(var i = 0, c = img_lazy_load_obj.img_map.length; i < c; i++) {
            
            if(img_lazy_load_obj.img_map[i] >= vis_height || img_lazy_load_obj.img_map[i] < vis_top_height) {
                continue;
            }

            var img_height_obj = img_lazy_load_obj.img_map[i];
            var img_cur_obj = img_lazy_load_obj.element_obj[img_height_obj];

            if(img_cur_obj == undefined) {
                continue;
            }
            
            for(var j = 0, c2 = img_cur_obj.length; j < c2; j++) {                
                //如果加载完成的数量小于等于最大加载数，
                if(img_lazy_load_obj.loading_number >= img_lazy_load_obj.max_loading){   
                    break;
                }                                
                
                img_lazy_load_obj.loading_number++;
                
                //图片预加载
                img_lazy_load_obj.img_load({
                    src: img_cur_obj[j].getAttribute(img_lazy_load_obj.load_atr),
                    obj: img_cur_obj[j]
                });
            }
            if(j >= c2){
                //该高度的已经加载完成
                img_lazy_load_obj.img_lazy_count--;
                delete img_lazy_load_obj.img_map[i];
            }
        }

        img_lazy_load_obj.flag = true;

        //进行图片加载
        img_lazy_load_obj.time_out = setTimeout(img_lazy_load_obj.img_lazy_down, 1000);
    }

    //图片预加载
    img_lazy_load_obj.img_load = function(param) {
        var img = new Image();        
        
        //加载完毕
        if(img.complete) {
            param.obj.src = param.src;                        
            param.obj.removeAttribute(img_lazy_load_obj.load_atr);                        
            img_lazy_load_obj.loading_number--;
            return;
        }

        img.onload = function(){
            param.obj.src = param.src;      
            param.obj.removeAttribute(img_lazy_load_obj.load_atr);                
            img_lazy_load_obj.loading_number--;
        }
        
        img.src = param.src;                
    }

    //获取图片所在高度
    img_lazy_load_obj.get_img_top = function(img_obj) {
        if(img_obj == null) {
            return null;
        }

        var img_top = img_obj.offsetTop;
        while(img_obj = img_obj.offsetParent) {
            img_top += img_obj.offsetTop;
        }                

        return img_top;
    }

    if(window.img_lazy_load_obj == undefined) {
        window.img_lazy_load_obj = img_lazy_load_obj;

        if(window.attachEvent) {
            window.attachEvent('onload', function() {
                img_lazy_load_obj.init({tags:['img']});
            });
        } else {
            window.addEventListener('load', function() {
                img_lazy_load_obj.init({tags:['img']});
            },true);
        }
    }
})();
